#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <math.h>
#include <qtextcodec.h>//
#include <stdio.h>
#include <QTextCodec>
#include <qdebug.h>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));
////    QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf8"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));
    people_number=0;
    people_page_now=0;
    people_page_num=0;
    user_number=0;
    administrator_number=0;

    hide_other();
    open_file();
    read_people_msg_file();
    read_user_msg_file();
    read_administrator_msg_file();
    people_page_now = 0;
}
void MainWindow::open_file() //创建或者打开表格（数据库）
{
    database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName("student_database.db");//链接、打开数据库
    if (!database.open())
    {
        qDebug() << "Error: Failed to connect database." << database.lastError();
    }
    else
    {
        qDebug() << "Succeed to connect database." ;
    }

    sql_query =QSqlQuery(database);//打开（创建）数据库中的表格
    //创建/打开表格
    if (database.tables().contains("student"))
    {
        sql_query.exec("open table student");
    }
    else
    {
        //姓名、生日、性别、专业、特长
            if(!sql_query.exec("create table student(name text primary key, brith text, sexual text, major1 text, speciality text)"))
            {
                qDebug() << "Error: Fail to create table."<< sql_query.lastError();
            }
            else
            {
                qDebug() << "Table created!";
            }
    }

    user_msg_file = fopen("user_msg.txt", "a+");                    //打开表格（文本方式）
    administrator_msg_file = fopen("administrator_msg.txt", "a+");  //打开表格
//    printf("%d %d %d \n", people_msg_file,user_msg_file,administrator_msg_file);
    if (user_msg_file < 0 || administrator_msg_file < 0)

        exit(0);
}
void MainWindow::hide_other()//隐藏登录不需要的控件
{
    ui->pushButton_3->hide();
    ui->pushButton_4->hide();
    ui->tableWidget->hide();
    ui->tableWidget_2->hide();
    ui->tableWidget_3->hide();
    ui->pushButton_5->hide();
    ui->pushButton_6->hide();
    ui->pushButton_7->hide();
    ui->pushButton_8->hide();
    ui->pushButton_9->hide();
    ui->pushButton_10->hide();
    ui->pushButton_11->hide();
    ui->pushButton_12->hide();
    ui->pushButton_13->hide();
    ui->pushButton_14->hide();
    ui->pushButton_15->hide();
    ui->pushButton_16->hide();
    ui->pushButton_17->hide();
    ui->pushButton_18->hide();
    ui->pushButton_19->hide();
    ui->label_5->hide();
    ui->label_4->hide();
    ui->label_6->hide();
    ui->label_7->hide();
    ui->label_8->hide();
    ui->label_9->hide();
    ui->label_10->hide();
    ui->lineEdit_3->hide();
    ui->lineEdit_4->hide();
    ui->lineEdit_5->hide();
    ui->lineEdit_6->hide();
    ui->lineEdit_7->hide();
}
void MainWindow::read_people_msg_file()//在数据库对应表格中读取人员信息
{
//    struct  People_info people_msg_temp;
    QString name;
    QString brith;
    QString sexual;
    QString major1;
    QString speciality;
    int i = 0;
    sql_query.exec("select * from student");//选择student表格中所有的人员并在下方读取存入
    if(!sql_query.exec())
    {
        qDebug()<<sql_query.lastError();
    }
    else
    {
        while(sql_query.next())
        {

            name = sql_query.value(0).toString();
            brith = sql_query.value(1).toString();
            sexual = sql_query.value(2).toString();
            major1 = sql_query.value(3).toString();
            speciality = sql_query.value(4).toString();
            All_people[i].name=name;
            All_people[i].brith=brith;
            All_people[i].sexual=sexual;
            All_people[i].major1=major1;
            All_people[i].speciality=speciality;
            i++;
            qDebug()<<QString("name:%1    birth:%2    sexual:%3    major1:%4    speciality:%5").arg(name).arg(brith).arg(sexual).arg(major1).arg(speciality);
        }
    }

    //计算一共有多少页数据（一页10个）
    people_number = i;
    people_page_num = int((i-1) / 10);

}
void MainWindow::show_people_msg_table()//在表格中显示数据
{
    clean_people__msg_table();//清空表格
    for (int i = 0; i < 10; i++)
    {
        if (people_page_now * 10 + i >= people_number)
            break;
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString(All_people[people_page_now * 10 + i].name)));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(QString(All_people[people_page_now * 10 + i].brith)));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(QString(All_people[people_page_now * 10 + i].sexual)));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(QString(All_people[people_page_now * 10 + i].major1)));
        ui->tableWidget->setItem(i,4,new QTableWidgetItem(QString(All_people[people_page_now * 10 + i].speciality)));
//        ui->tableWidget->item(i, 0)->setText(QString(QLatin1String(All_people[people_page_now * 10 + i].name)));
    }
    set_current_peopleMsg_page();//设置当前页数

}
void MainWindow::clean_people__msg_table()//清空表格显示当前页
{
    for (int i = 0; i < 10; i++)
    {
        for ( int j = 0; j < 5; j++)
            ui->tableWidget->setItem(i,j,new QTableWidgetItem(""));
    }
}
void MainWindow::delet_oneperson()//删除一个人
{
    int curRow = ui->tableWidget->currentRow();
    QString delet_name = All_people[people_page_now * 10 + curRow].name;//获取人姓名
    for (int i = 0; i < 5; i++)
    {
        ui->tableWidget->setItem(curRow,i,new QTableWidgetItem(""));
    }
    for (int i = people_page_now * 10 + curRow; i < people_number; i++)//
    {
        All_people[i].name=All_people[i+1].name;
        All_people[i].brith=All_people[i+1].brith;
        All_people[i].sexual=All_people[i+1].sexual;
        All_people[i].major1=All_people[i+1].major1;
        All_people[i].speciality=All_people[i+1].speciality;
    }
    people_number -= 1;
    people_page_num = int((people_number-1) / 10);
    if (people_page_num < people_page_now)
    {
        people_page_now = people_page_num;
    }

    sql_query.exec(QString("delete from student where name = '%1'").arg(delet_name));
    if(!sql_query.exec())
    {
        qDebug()<<sql_query.lastError();
    }
    else
    {
        qDebug()<<"deleted!";
    }
    show_people_msg_table();
    set_current_peopleMsg_page();
}

void MainWindow::add_oneperson()//添加一个人
{
    QString name_Qstring = ui->tableWidget_2 ->item(0,0)->text();
    if (!name_Qstring.isEmpty() )
    {
        QString brith_Qstring = ui->tableWidget_2->item(0,1)->text();
        QString sexual_Qstring = ui->tableWidget_2->item(0,2)->text();
        QString major1_Qstring = ui->tableWidget_2->item(0,3)->text();
        QString speciality_Qstring = ui->tableWidget_2->item(0,4)->text();

//        QByteArray name;
//        QByteArray brith;
//        QByteArray sexual;
//        QByteArray major1;
//        QByteArray speciality;
//        name.append(name_Qstring);     //也可以 ba2 = s2.toLatin1();
//        brith.append(brith_Qstring);
//        sexual.append(sexual_Qstring);
//        major1.append(major1_Qstring);
//        speciality.append(speciality_Qstring);

        All_people[people_number].name=name_Qstring;
        All_people[people_number].brith=brith_Qstring;
        All_people[people_number].sexual=sexual_Qstring;
        All_people[people_number].major1=major1_Qstring;
        All_people[people_number].speciality=speciality_Qstring;
        people_number += 1;
        people_page_num = int((people_number-1) / 10);
        people_page_now = people_page_num;
        if(!sql_query.exec(QString("INSERT INTO student VALUES('%1', '%2',  '%3', '%4', '%5')")
                                    .arg(name_Qstring).arg(brith_Qstring).arg(sexual_Qstring).arg(major1_Qstring).arg(speciality_Qstring)))
        {
            qDebug() << sql_query.lastError();
        }
        else
        {
            qDebug() << "inserted Wang!";
        }
        show_people_msg_table();
    }

}

void MainWindow::people_masg_next_page()//显示下一页
{
    if ( people_page_now != people_page_num)
    {
        clean_people__msg_table();
        people_page_now += 1;
        show_people_msg_table();
        set_current_peopleMsg_page();
    }
}
void MainWindow::people_masg_last_page()//显示上一页
{
    if ( people_page_now != 0)
    {
        clean_people__msg_table();
        people_page_now -= 1;
        show_people_msg_table();
        set_current_peopleMsg_page();
    }
}
void MainWindow::people_masg_end_page()//显示最后一页
{
    clean_people__msg_table();
    people_page_now = people_page_num;
    show_people_msg_table();
    set_current_peopleMsg_page();
}
void MainWindow::people_masg_first_page()//显示第一页
{
    clean_people__msg_table();
    people_page_now = 0;
    show_people_msg_table();
}

void MainWindow::people_masg_page_jump()//跳转到多少页
{

    QString Jump_page_Qstring = ui->lineEdit_3->text();//获取跳转页数
    QByteArray Jump_page_text = Jump_page_Qstring.toLatin1();
    char* Jump_page = Jump_page_text.data();
    int page_num = char_to_int(Jump_page);
    people_page_now = page_num-1;
    show_people_msg_table();
    set_current_peopleMsg_page();
}

//void MainWindow::write_people_msg_to_file()
//{
//    fclose(people_msg_file);
//    people_msg_file = fopen("people_msg.txt", "w+");
//    for (int i = 0; i < 1000; i++)
//    {
////        if (strlen(All_people[i].name) == 0)
////            break;
////        fprintf(people_msg_file,"%s %s %s %s %s\n", All_people[i].name, All_people[i].brith, All_people[i].sexual,All_people[i].major1, All_people[i].speciality);
//    }
//    fclose(people_msg_file);
//    people_msg_file = fopen("people_msg.txt", "a+");


//}
void MainWindow::save_edited_people_masg()//保存当前页数修改的人员信息
{
    for (int j = 0; j < 10; j++)
    {
        QString name_Qstring = ui->tableWidget->item(j,0)->text();
        QString brith_Qstring = ui->tableWidget->item(j,1)->text();
        QString sexual_Qstring = ui->tableWidget->item(j,2)->text();
        QString major1_Qstring = ui->tableWidget->item(j,3)->text();
        QString speciality_Qstring = ui->tableWidget->item(j,4)->text();
//        QByteArray name;
//        QByteArray brith;
//        QByteArray sexual;
//        QByteArray major1;
//        QByteArray speciality;
//        name.append(name_Qstring);     //也可以 ba2 = s2.toLatin1();
//        brith.append(brith_Qstring);
//        sexual.append(sexual_Qstring);
//        major1.append(major1_Qstring);
//        speciality.append(speciality_Qstring);
        All_people[people_page_now*10 + j].name=name_Qstring;
        All_people[people_page_now*10 + j].brith=brith_Qstring;
        All_people[people_page_now*10 + j].sexual=sexual_Qstring;
        All_people[people_page_now*10 + j].major1=major1_Qstring;
        All_people[people_page_now*10 + j].speciality=speciality_Qstring;

        sql_query.exec(QString("update student set brith = '%1' where name = '%2'").arg(brith_Qstring).arg(name_Qstring));
        sql_query.exec(QString("update student set sexual = '%1' where name = '%2'").arg(sexual_Qstring).arg(name_Qstring));
        sql_query.exec(QString("update student set major1 = '%1' where name = '%2'").arg(major1_Qstring).arg(name_Qstring));
        sql_query.exec(QString("update student set speciality = '%1' where name = '%2'").arg(speciality_Qstring).arg(name_Qstring));
        if(!sql_query.exec())
        {
            qDebug() << sql_query.lastError();
        }
        else
        {
            qDebug() << "updated!";
        }
    }
//    write_people_msg_to_file();
    show_people_msg_table();
}
void MainWindow::set_current_peopleMsg_page()//设置显示当前的页数
{
    char people_page_num_str[10];
    char people_page_now_str[10];
    sprintf(people_page_num_str,"/%d 页",people_page_num+1);
    sprintf(people_page_now_str,"%d",people_page_now+1);
    ui->label_5->setText(QString(people_page_num_str));
    ui->lineEdit_3->setText(QString(people_page_now_str));
//    printf("%d %d %s\n",people_number, people_page_num, people_page_num_str);
}

void MainWindow::on_click_manager_people_msg()//当点击管理人员信息时，显示一部分控件，隐藏一部分控件
{
    ui->pushButton_3->hide();
    ui->pushButton_4->hide();
    ui->tableWidget->show();
    ui->tableWidget_2->show();
    ui->pushButton_5->show();
    ui->pushButton_6->show();
    ui->pushButton_8->show();
    ui->pushButton_9->show();
    ui->pushButton_10->show();
    ui->pushButton_11->show();
    ui->pushButton_12->show();
    ui->pushButton_13->show();
    ui->pushButton_14->show();
    ui->label_4->show();
    ui->label_5->show();
    ui->lineEdit_3->show();
    people_page_now = 0;
    set_current_peopleMsg_page();
    show_people_msg_table();

}
void MainWindow::back_to_main_menu()//返回主菜单
{
    ui->pushButton_3->show();
    ui->pushButton_4->show();
    ui->tableWidget->hide();
    ui->tableWidget_2->hide();
    ui->tableWidget_3->hide();
    ui->pushButton_5->hide();
    ui->pushButton_6->hide();
    ui->pushButton_8->hide();
    ui->pushButton_9->hide();
    ui->pushButton_10->hide();
    ui->pushButton_11->hide();
    ui->pushButton_12->hide();
    ui->pushButton_13->hide();
    ui->pushButton_14->hide();
    ui->pushButton_15->hide();
    ui->pushButton_16->hide();
    ui->pushButton_17->hide();
    ui->pushButton_18->hide();
    ui->pushButton_19->hide();
    ui->label_4->hide();
    ui->label_5->hide();
    ui->label_6->hide();
    ui->label_7->hide();
    ui->label_8->hide();
    ui->label_9->hide();
    ui->label_10->hide();
    ui->lineEdit_3->hide();
    ui->lineEdit_4->hide();
    ui->lineEdit_5->hide();
    ui->lineEdit_6->hide();
    ui->lineEdit_7->hide();
}





void MainWindow::read_user_msg_file()//读取已经保存的用户的信息，包括用户名和密码
{
    char ID[40];
    char user_password[40];
    const char* zerostring = "";
    int i = 0;
    for (i = 0; i < 8; i++)
    {
        fscanf(user_msg_file,"%s %s", ID, user_password);
        if (strlen(ID) == 0)
            break;
        strcpy(All_user[i].ID,ID);
        strcpy(All_user[i].user_password,user_password);
        strcpy(ID, zerostring);
        strcpy(user_password, zerostring);
    }
    user_number = i;
}
void MainWindow::read_administrator_msg_file()//读取已经保存的管理员的信息，包括用户名和密码
{
    char ID[40];
    char user_password[40];
    const char* zerostring = "";
    int i = 0;
    for (i = 0; i < 8; i++)
    {
        fscanf(administrator_msg_file,"%s %s", ID, user_password);
        if (strlen(ID) == 0)
            break;
        strcpy(All_administrator[i].ID,ID);
        strcpy(All_administrator[i].user_password,user_password);
//        printf("%s %s\n", All_administrator[i].ID, All_administrator[i].user_password);
        strcpy(ID, zerostring);
        strcpy(user_password, zerostring);
    }
    administrator_number = i;
}

void MainWindow::show_users_table()//显示普通用户登录后的操作界面
{
    clean_users_table();
    for (int i = 0; i < 8; i++)
    {
        if (i >= administrator_number)
            break;
        ui->tableWidget_3->setItem(i,0,new QTableWidgetItem(QString(All_administrator[i].ID)));
    }
    for (int i = 0; i < 8; i++)
    {
        if (i >= user_number)
            break;
//        printf("%d \n", user_number);
        ui->tableWidget_3->setItem(i,1,new QTableWidgetItem(QString(All_user[i].ID)));
    }
}
void MainWindow::add_user()//添加一名普通用户（帐号密码）
{
    QString ID_Qstring = ui->lineEdit_6->text();
    if (!ID_Qstring.isEmpty() && user_number < 8)
    {
        QString user_password_Qstring = ui->lineEdit_7->text();
        QByteArray ID;
        QByteArray user_password;

        ID.append(ID_Qstring);     //也可以 ba2 = s2.toLatin1();
        user_password.append(user_password_Qstring);

        strcpy(All_user[user_number].ID,ID.data());
        strcpy(All_user[user_number].user_password,user_password.data());

        user_number += 1;
//            printf("%d %d\n",people_number, people_page_num);
        write_user_msg_to_file();
        show_users_table();
    }
}
void MainWindow::add_administrator()//添加一名管理员（帐号密码）
{
    QString ID_Qstring = ui->lineEdit_4->text();
    if (!ID_Qstring.isEmpty() && administrator_number < 8)
    {
        QString user_password_Qstring = ui->lineEdit_5->text();
        QByteArray ID;
        QByteArray user_password;

        ID.append(ID_Qstring);     //也可以 ba2 = s2.toLatin1();
        user_password.append(user_password_Qstring);

        strcpy(All_administrator[administrator_number].ID,ID.data());
        strcpy(All_administrator[administrator_number].user_password,user_password.data());
\
        administrator_number += 1;
//            printf("%d %d\n",people_number, people_page_num);
        write_administrator__msg_to_file();
        show_users_table();
    }
}
void MainWindow::delet_user()//删除一名普通用户
{
    int curRow = ui->tableWidget_3->currentRow();
    int curColumn = ui->tableWidget_3->currentColumn();
    ui->tableWidget_3->setItem(curRow,curColumn,new QTableWidgetItem(""));
    if (curColumn == 1)
    {
        for (int i = curRow; i < user_number; i++)
        {
            strcpy(All_user[i].ID,All_user[i+1].ID);
            strcpy(All_user[i].user_password,All_user[i+1].user_password);
        }
        user_number -= 1;
        write_user_msg_to_file();

    }
    else if (curColumn == 0)
    {
        for (int i = curRow; i < administrator_number; i++)
        {
            strcpy(All_administrator[i].ID,All_administrator[i+1].ID);
            strcpy(All_administrator[i].user_password,All_administrator[i+1].user_password);
        }
        administrator_number -= 1;
        write_administrator__msg_to_file();

    }
    show_users_table();
}

void MainWindow::clean_users_table()//清理删除后的已有用户列表
{
    for (int i = 0; i < 8; i++)
    {
        for ( int j = 0; j < 2; j++)
            ui->tableWidget_3->setItem(i,j,new QTableWidgetItem(""));
    }
}
void MainWindow::write_user_msg_to_file()//写入一名普通用户到文件系统
{
    fclose(user_msg_file);
    user_msg_file = fopen("user_msg.txt", "w+");
    for (int i = 0; i < 8; i++)
    {
        if (i >= user_number)
            break;
        if (strlen(All_user[i].ID) == 0)
            break;
        fprintf(user_msg_file,"%s %s\n", All_user[i].ID, All_user[i].user_password);
    }
    fclose(user_msg_file);
    user_msg_file = fopen("user_msg.txt", "a+");
}
void MainWindow::write_administrator__msg_to_file()//写入一名管理员用户到文件系统
{
    fclose(administrator_msg_file);
    administrator_msg_file = fopen("administrator_msg.txt", "w+");
    for (int i = 0; i < 8; i++)
    {
        if (i >= administrator_number)
            break;
        if (strlen(All_administrator[i].ID) == 0)
            break;
        fprintf(administrator_msg_file,"%s %s\n", All_administrator[i].ID, All_administrator[i].user_password);
    }
    fclose(administrator_msg_file);
    administrator_msg_file = fopen("user_msg.txt", "a+");
}


void MainWindow::on_click_edit_user_and_administrator_msg()//当点击管理员用户登录的时候，显示管理员操作界面
{
    ui->pushButton_3->hide();
    ui->pushButton_4->hide();
    ui->tableWidget_3->show();
    ui->lineEdit_4->show();
    ui->lineEdit_5->show();
    ui->lineEdit_6->show();
    ui->lineEdit_7->show();
    ui->pushButton_15->show();
    ui->pushButton_16->show();
    ui->pushButton_17->show();
    ui->pushButton_18->show();
    ui->label_6->show();
    ui->label_7->show();
    ui->label_8->show();
    ui->label_9->show();
    ui->label_10->show();
    show_users_table();
}

void MainWindow::on_click_view_people_msg()//点击查看人员信息（普通用户模式）
{
    ui->pushButton_6->hide();
    ui->pushButton_3->hide();
    ui->pushButton_4->hide();
    ui->tableWidget->show();
//    ui->tableWidget_2->show();
    ui->pushButton_5->show();
    ui->pushButton_6->show();
    ui->pushButton_7->hide();
//    ui->pushButton_8->show();
//    ui->pushButton_9->show();
//    ui->pushButton_10->show();
    ui->pushButton_11->show();
    ui->pushButton_12->show();
    ui->pushButton_13->show();
    ui->pushButton_19->show();
//    ui->pushButton_14->show();
//    ui->label_4->show();
    ui->label_5->show();
    ui->lineEdit_3->show();
    people_page_now = 0;
    set_current_peopleMsg_page();
    show_people_msg_table();

}
void MainWindow::normal_user_back_to_main_menu()//普通用户返回主菜单
{

    ui->tableWidget->hide();
//    ui->tableWidget_2->show();
    ui->pushButton_5->hide();
    ui->pushButton_6->hide();
    ui->pushButton_7->show();
//    ui->pushButton_8->show();
//    ui->pushButton_9->show();
    ui->pushButton_10->hide();
    ui->pushButton_11->hide();
    ui->pushButton_12->hide();
    ui->pushButton_13->hide();
    ui->pushButton_19->hide();
//    ui->pushButton_14->show();
//    ui->label_4->show();
    ui->label_5->hide();
    ui->lineEdit_3->hide();
    people_page_now = 0;
}
MainWindow::~MainWindow()
{
    delete ui;
    fclose(people_msg_file);
    fclose(user_msg_file);
    fclose(administrator_msg_file);
}

void MainWindow::affirm_user_msg()//确认普通用户帐号密码是否正确
{
    QString text_info = ui->lineEdit->text();
    QString text_info2 = ui->lineEdit_2->text();
    QByteArray text;
    QByteArray text2;
    text.append(text_info);     //也可以 ba2 = s2.toLatin1();
    text2.append(text_info2);
    const char *ID = text.data();
    const char *password = text2.data();
//    printf("%s\n", password);
    int i = 0;
    for (i = 0; i < user_number; i++)
    {
//        printf("21\n");
        if ( !strcmp(ID,All_user[i].ID) && !strcmp(password,All_user[i].user_password))
        {
//             printf("11\n");
            ui->lineEdit->close();
            ui->lineEdit_2->close();
            ui->label->close();
            ui->label_2->close();
            ui->label_3->close();
            ui->pushButton->close();
            ui->pushButton_2->close();
            ui->pushButton_7->show();
            break;
        }
    }
    if (i == user_number)
//         printf("12\n");
        ui->label_3->setText(tr("ID/Password error!"));
}

void MainWindow::affirm_administrator_msg()//确认管理员用户帐号密码是否正确
{
//    ui->pushButton_3->show();
//    ui->pushButton_4->show();
    QString text_info = ui->lineEdit->text();
    QString text_info2 = ui->lineEdit_2->text();
    QByteArray text;
    QByteArray text2;
    text.append(text_info);     //也可以 ba2 = s2.toLatin1();
    text2.append(text_info2);
    const char *ID = text.data();
    const char *password = text2.data();
//    printf("%s %s %d \n", password,ID, administrator_number);
    int i = 0;
    for (i = 0; i < administrator_number; i++)
    {
//        printf("%s %s\n",All_administrator[i].user_password, All_administrator[i].ID);
        if ( !strcmp(ID,All_administrator[i].ID) && !strcmp(password,All_administrator[i].user_password))
        {
//            printf("11\n");
            ui->lineEdit->close();
            ui->lineEdit_2->close();
            ui->label->close();
            ui->label_2->close();
            ui->label_3->close();
            ui->pushButton->close();
            ui->pushButton_2->close();
            ui->pushButton_3->show();
            ui->pushButton_4->show();
            break;
        }
    }
    if ( i == administrator_number)
//        printf("12\n");
        ui->label_3->setText(QString("ID/Password error!"));
}



int MainWindow::char_to_int(const char* data)//把字符串转化为int
{
    int Key_Buf = 0;
    for (int i = 0; i < strlen(data); i++)
    {
        switch(data[strlen(data)-i-1])
        {
            case '0':Key_Buf += 0 * pow(10, i);break;
            case '1':Key_Buf += 1 * pow(10, i);break;
            case '2':Key_Buf += 2 * pow(10, i);break;
            case '3':Key_Buf += 3 * pow(10, i);break;
            case '4':Key_Buf += 4 * pow(10, i);break;
            case '5':Key_Buf += 5 * pow(10, i);break;
            case '6':Key_Buf += 6 * pow(10, i);break;
            case '7':Key_Buf += 7 * pow(10, i);break;
            case '8':Key_Buf += 8 * pow(10, i);break;
            case '9':Key_Buf += 9 * pow(10, i);break;
            default:

                return -1;
        }
    }
    return Key_Buf;
}
